package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;

import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.common.BaseActivity;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;


public class OnlineShopActivity extends BaseActivity implements OnClickListener {

    // Content View Elements
    private ScrollView scrollView;
    private LinearLayout group_buy;
    private LinearLayout promotion;
    private LinearLayout catering;
    private LinearLayout fruit;
    private LinearLayout meat;
    private LinearLayout snacks;
    private LinearLayout supermarket;
    private LinearLayout housekeeping;
    private LinearLayout carwashing;
    private LinearLayout more;
    private com.saifan.wyy_ov.ui.view.NoScrollListView lv_shop;

    private TextView moreStore;

    GetData getData;
    private CommonDataBean request;
    protected List<StoreBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_shop);
        bindViews();
        setEnabledNavigation(true);
        bindLisenter();
        getData = new GetData();
        request = new CommonDataBean();
        request.setYXDID(getRoomInfoBean().getXMBS());
        getData = new GetData();
        bindData();

    }

    private void bindData() {
        request.setPage(0);
        getData.getDataJson(this, Api.StoreList.STORELIST, request, "", new ResponseData() {

            @Override
            public void onSuccess(String json) {

                data = (new Gson()).fromJson(json, new TypeToken<List<StoreBean>>() {
                }.getType());

                if (data != null && data.size() > 0) {
                    CommonAdapter<StoreBean> adapter = new CommonAdapter<StoreBean>(OnlineShopActivity.this, data, R.layout.onlinemall_list_item) {
                        @Override
                        public void convert(ViewHolderHelper helper, StoreBean item) {
                            helper.setText(R.id.show_name, StringUtil.returnShow(item.getStoreName()));
                            helper.setText(R.id.week, StringUtil.returnShow(item.getBusinessDate()).replace(",", "至"));
                            helper.setText(R.id.time, StringUtil.returnShow(item.getBusinessTime()).replace(",", "至"));
                            if (StringUtil.isNullOrEmpty(item.getDeliveryInstructions())) {
                                helper.setText(R.id.tv_DeliveryInstructions, "不支持外送");
                            } else {
                                helper.setText(R.id.tv_DeliveryInstructions, StringUtil.returnShow(item.getDeliveryInstructions() + ""));
                            }
                            helper.setText(R.id.tv_DeliveryAmount, "月销量:" + item.getDeliveryAmount());
                            helper.setImageByUrl(R.id.iv_shop_img, item.getHeadPhoto());
                            if (item.isIsDelivery()) {
                                helper.setImageResource(R.id.iv_isDelivery, R.drawable.label_song);
                            }else {
                                helper.setImageResource(R.id.iv_isDelivery, R.drawable.alpha);
                            }


                        }
                    };

                    lv_shop.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            Intent intent = new Intent(OnlineShopActivity.this, StoreDetailsActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("data", data.get(arg2));
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                    lv_shop.setAdapter(adapter);
                }


            }

            @Override
            public void onFailed(String str) {

            }

        });

    }


    private void bindViews() {

         scrollView = (ScrollView) findViewById(R.id.scrollview);
        group_buy = (LinearLayout) findViewById(R.id.group_buy);
        promotion = (LinearLayout) findViewById(R.id.promotion);
        catering = (LinearLayout) findViewById(R.id.catering);
        fruit = (LinearLayout) findViewById(R.id.fruit);
        meat = (LinearLayout) findViewById(R.id.meat);
        snacks = (LinearLayout) findViewById(R.id.snacks);
        supermarket = (LinearLayout) findViewById(R.id.supermarket);
        housekeeping = (LinearLayout) findViewById(R.id.housekeeping);
        carwashing = (LinearLayout) findViewById(R.id.carwashing);
        more = (LinearLayout) findViewById(R.id.more);
        moreStore = (TextView ) findViewById(R.id.more_store);
        lv_shop = (com.saifan.wyy_ov.ui.view.NoScrollListView) findViewById(R.id.online_list);
        lv_shop.setFocusable(false);
    }

    public void bindLisenter() {
        group_buy.setOnClickListener(this);
        promotion.setOnClickListener(this);
        catering.setOnClickListener(this);
        fruit.setOnClickListener(this);
        meat.setOnClickListener(this);
        snacks.setOnClickListener(this);
        supermarket.setOnClickListener(this);
        housekeeping.setOnClickListener(this);
        carwashing.setOnClickListener(this);
        more.setOnClickListener(this);
        moreStore.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.menu_collect) {//进入收藏页面
            Intent intentGoMyCollect = new Intent(OnlineShopActivity.this, MyCollectActivity.class);
            startActivity(intentGoMyCollect);

        } else if (id == R.id.menu_indent) {
            Intent intentGoMyOrder = new Intent(OnlineShopActivity.this, MyOrderActivity.class);
            startActivity(intentGoMyOrder);

        } else if (id == R.id.menu_cart) {
            Intent intentGoCart = new Intent(OnlineShopActivity.this, MyCartActivity.class);
            startActivity(intentGoCart);

        } else {
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    public void onClick(View v) {
        int key = v.getId();
        Intent intent = new Intent(OnlineShopActivity.this, AroundMallActivity.class);

        if (key == R.id.group_buy) {// 跳转到周边团购
            Intent intentGoGroupBuyActivity = new Intent(this, GroupBuyActivity.class);

            intentGoGroupBuyActivity.putExtra("Type", 1);

            startActivity(intentGoGroupBuyActivity);

        } else if (key == R.id.promotion) {// 跳转到促销

            Intent intentPromotion = new Intent(this, PromotionActivity.class);
            intentPromotion.putExtra("Type", 2);

            startActivity(intentPromotion);

        } else if (key == R.id.catering) {// 跳转到餐饮界面
            intent.putExtra("category", "餐饮");
            startActivity(intent);

        } else if (key == R.id.fruit) {// 跳转到果蔬界面
            intent.putExtra("category", "果蔬");
            startActivity(intent);

        } else if (key == R.id.meat) {// 跳转到肉类界面
            intent.putExtra("category", "肉类");
            startActivity(intent);

        } else if (key == R.id.snacks) {// 跳转到零食界面
            intent.putExtra("category", "零食");
            startActivity(intent);

        } else if (key == R.id.supermarket) {// 跳转到超市界面
            intent.putExtra("category", "便利超市");
            startActivity(intent);

        } else if (key == R.id.housekeeping) {// 跳转到家电界面
            intent.putExtra("category", "家政");
            startActivity(intent);

        } else if (key == R.id.carwashing) {// 跳转到烟酒界面
            intent.putExtra("category", "烟酒");
            startActivity(intent);

        } else if (key == R.id.more) {// 跳转到更多界面
            intent = new Intent(OnlineShopActivity.this, StoreCategoryActivity.class);
            startActivity(intent);

        } else if (key == R.id.more_store) {// 跳转到周边店铺
            intent.putExtra("category", "全部");
            startActivity(intent);


        } else {
        }

    }

}
